#if defined(WEBRTC_OHOS)
#include "modules/video_render/ohos/ohos_video_render_ohos.h"
#endif
#include "video_render_manager_impl.h"

namespace webrtc {
VideoRenderManager* VideoRenderManager::GetInstance() {
  static VideoRenderManager *inst = nullptr;
  if (!inst) {
    inst = new VideoRenderManagerImpl;
  }
  return inst;
}

std::shared_ptr<VideoRenderInterface> VideoRenderManagerImpl::CreateRender(const std::string& id) {
  std::unique_lock<std::mutex> lock(mutex_);
  auto it = video_render_map_.find(id);
  if (it != video_render_map_.end()) {
    return it->second;
  } else {
    std::shared_ptr<VideoRenderInterface> new_render = nullptr;
    #if defined(WEBRTC_OHOS)
      new_render = std::make_shared<VideoRenderOhos>();
    #endif    
    video_render_map_[id] = new_render;
    return new_render;
  }
}

std::shared_ptr<VideoRenderInterface> VideoRenderManagerImpl::GetRender(const std::string& id) {
  std::unique_lock<std::mutex> lock(mutex_);
  auto it = video_render_map_.find(id);
  if (it != video_render_map_.end()) {
    return it->second;
  }
  return nullptr;
}

void VideoRenderManagerImpl::DisposeRender(const std::string& id) {
  std::unique_lock<std::mutex> lock(mutex_);
  auto it = video_render_map_.find(id);
  if (it != video_render_map_.end()) {
    it->second->Release();
    video_render_map_.erase(it);
  }
}
}